home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume4 / 8to1 next >
Encoding:
Text File  |  1989-02-03  |  27.7 KB  |  951 lines

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: rainbow@sbcs.sunysb.edu
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i127: SUN raster file color to mono converter.
  5. Message-ID: <8810151959.AA25566@sbgrad1>
  6. Date: 22 Oct 88 01:16:40 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: rainbow@sbcs.sunysb.edu
  9. Lines: 939
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 127
  13. Submitted-by: "A. Nonymous" <rainbow@sbcs.sunysb.edu>
  14. Archive-name: 8to1
  15.  
  16. #! /bin/sh
  17. # This is a shell archive.  Remove anything before this line, then unpack
  18. # it by saving it into a file and typing "sh file".  To overwrite existing
  19. # files, type "sh file -c".  You can also feed this as standard input via
  20. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  21. # will see the following message at the end:
  22. #        "End of archive 1 (of 1)."
  23. # Contents:  Makefile README 8to1.c scload.c
  24. # Wrapped by rainbow@sbgrad1 on Sat Oct 15 15:52:11 1988
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. if test -f Makefile -a "${1}" != "-c" ; then 
  27.   echo shar: Will not over-write existing file \"Makefile\"
  28. else
  29. echo shar: Extracting \"Makefile\" \(162 characters\)
  30. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  31. XBASE_DIR = $(PWD)
  32. XCFLAGS   = -O
  33. XLIBS     = -lpixrect
  34. X
  35. X
  36. X
  37. Xscload: 8to1
  38. X    $(CC) $(CFLAGS) -o scload scload.c $(LIBS)
  39. X
  40. X8to1:
  41. X    $(CC) $(CFLAGS) -o 8to1 8to1.c $(LIBS)
  42. X
  43. X
  44. END_OF_Makefile
  45. if test 162 -ne `wc -c <Makefile`; then
  46.     echo shar: \"Makefile\" unpacked with wrong size!
  47. fi
  48. # end of overwriting check
  49. fi
  50. if test -f README -a "${1}" != "-c" ; then 
  51.   echo shar: Will not over-write existing file \"README\"
  52. else
  53. echo shar: Extracting \"README\" \(1209 characters\)
  54. sed "s/^X//" >README <<'END_OF_README'
  55. X   The program 8to1 converts a 8-bit depth sun raster file (both color, 
  56. X   i.e. rgb are different, and grey, i.e. rgb are the same, and both 
  57. X   standard format and byte-encoded format) to a 1-bit depth mono sun
  58. X   raster file. If your color image has no colormap, this program would
  59. X   provide a default grey scale color map. It implements several halftone
  60. X   algorithms from Digital Halftones by Dot Diffusion in acm Transactions 
  61. X   on Graphics, such as error diffusion, ordered dither and dot diffusion
  62. X   with and without edge enhancement.  Just type 8to1, it will give you
  63. X   the usage.
  64. X
  65. X   The program "scload" is like Sun's "screenload" program, but more
  66. X   entertaining, and actually better than screenload. It's written
  67. X   by Raymond Kreisel, the author of Touchup.
  68. X
  69. X
  70. X   The program 8to1 was written by :
  71. X
  72. X    Hong Min 
  73. X    Computer Science Department
  74. X       SUNY at Stony Brook 
  75. X    e-mail address : 
  76. X        UUCP: {allegra, philabs, pyramid, research}!sbcs!rainbow
  77. X        ARPA-Internet: rainbow@sbcs.sunysb.edu
  78. X        CSnet: rainbow@suny-sb
  79. X
  80. X   Everyone is welcome to write to me if you like the program or not.
  81. X   And welcome to optimize the code, as I haven't fooled with it to
  82. X   make it efficient yet.
  83. X
  84. X        Enjoy!                            
  85. X
  86. END_OF_README
  87. if test 1209 -ne `wc -c <README`; then
  88.     echo shar: \"README\" unpacked with wrong size!
  89. fi
  90. # end of overwriting check
  91. fi
  92. if test -f 8to1.c -a "${1}" != "-c" ; then 
  93.   echo shar: Will not over-write existing file \"8to1.c\"
  94. else
  95. echo shar: Extracting \"8to1.c\" \(16939 characters\)
  96. sed "s/^X//" >8to1.c <<'END_OF_8to1.c'
  97. X/**************************************************************************
  98. X Copyright (c) 1988 by Hong Min
  99. X
  100. X   compile : cc -O -o 8to1 8to1.c -lpixrect 
  101. X
  102. X   The program 8to1 converts a 8-bit depth sun raster file (both color, 
  103. X   i.e. rgb are different, and grey, i.e. rgb are the same, and both 
  104. X   standard format and byte-encoded format) to a 1-bit depth mono sun
  105. X   raster file. If your color image has no colormap, this program would
  106. X   provide a default grey scale color map. It implements several halftone
  107. X   algorithms from Digital Halftones by Dot Diffusion in acm Transactions 
  108. X   on Graphics, such as error diffusion, ordered dither and dot diffusion
  109. X   with and without edge enhancement.  Just type 8to1, it will give you
  110. X   the usage.
  111. X
  112. X   This program was written by :
  113. X
  114. X    Hong Min 
  115. X    Computer Science Department
  116. X       SUNY at Stony Brook 
  117. X    e-mail address : 
  118. X        UUCP: {allegra, philabs, pyramid, research}!sbcs!rainbow
  119. X        ARPA-Internet: rainbow@sbcs.sunysb.edu
  120. X        CSnet: rainbow@suny-sb
  121. X
  122. X   Everyone is welcome to write to me if you like the program or not.
  123. X   And welcome to optimize the code, as I haven't fooled with it to
  124. X   make it efficient yet.
  125. X
  126. X        Enjoy!                            
  127. X
  128. X****************************************************************************/
  129. X
  130. X
  131. X
  132. X#include <stdio.h>
  133. X#include <sys/types.h>
  134. X#include <pixrect/pixrect.h>
  135. X#include <pixrect/memvar.h>
  136. X#include <pixrect/pr_util.h>
  137. X#include <pixrect/pr_io.h>
  138. X#include <rasterfile.h>
  139. X
  140. X#define ORDER   16      /* dither matrix order */
  141. X#define WHITE   0       /* background color */
  142. X#define BLACK   ~0      /* foreground color */
  143. X#define FALSE    0
  144. X#define TRUE    1
  145. X#define ALPHA    7
  146. X#define BETA    3
  147. X#define GAMMA    5
  148. X#define DELTA    1
  149. X
  150. Xshort    **A, **B;
  151. Xint    ordered_dither[8][8] = { 0, 32,  8, 40,  2, 34, 10, 42,
  152. X                48, 16, 56, 24, 50, 18, 58, 26,
  153. X                12, 44,  4, 36, 14, 46,  6, 38,
  154. X                60, 28, 52, 20, 62, 30, 54, 22,
  155. X                 3, 35, 11, 43,  1, 33,  9, 41,
  156. X                51, 19, 59, 27, 49, 17, 57, 25,
  157. X                15, 47,  7, 39, 13, 45,  5, 37,
  158. X                63, 31, 55, 23, 61, 29, 53, 21 };
  159. Xint    dot_diffusion[8][8] = {    34, 48, 40, 32, 29, 15, 23, 31,
  160. X                42, 58, 56, 53, 21,  5,  7, 10,
  161. X                50, 62, 61, 45, 13,  1,  2, 18,
  162. X                38, 46, 54, 37, 25, 17,  9, 26,
  163. X                28, 14, 22, 30, 35, 49, 41, 33,
  164. X                20,  4,  6, 11, 43, 59, 57, 52,
  165. X                12,  0,  3, 19, 51, 63, 60, 44,
  166. X                24, 16,  8, 27, 39, 47, 55, 36 };
  167. Xint    reverse_matrix[64] = {    49, 21, 22, 50, 41, 13, 42, 14,
  168. X                58, 30, 15, 43, 48, 20, 33,  5, 
  169. X                57, 29, 23, 51, 40, 12, 34,  6,
  170. X                56, 28, 31, 59, 32,  4, 35,  7,
  171. X                 3, 39,  0, 36, 63, 27, 24, 60,
  172. X                 2, 38,  8, 44, 55, 19, 25, 61,
  173. X                 1, 37, 16, 52, 47, 11, 26, 62,
  174. X                10, 46,  9, 45, 54, 18, 17, 53 };
  175. X
  176. X
  177. Xmain(argc, argv)
  178. Xint    argc;
  179. Xchar    *argv[];
  180. X{
  181. Xregister colormap_t         colormap;
  182. Xregister unsigned char        *map;
  183. Xregister struct rasterfile    rh;
  184. Xregister Pixrect         *pr = 0;
  185. Xregister int            i, default_map = FALSE;
  186. Xint                option;
  187. X
  188. X    if ((argc > 4) || (argc < 2)) {
  189. X       fprintf(stderr, "Usage: %s option [infile [outfile]]\n", argv[0]);
  190. X       fprintf(stderr, "option : 0 -- error diffusion for grey scale\n");
  191. X       fprintf(stderr, "         1 -- ordered dither for grey scale\n");
  192. X       fprintf(stderr, "         2 -- dot diffusion for grey scale\n");
  193. X       fprintf(stderr, "         3 -- error diffusion for grey scale with edge enhancement\n");
  194. X       fprintf(stderr, "         4 -- ordered dither for grey scale with edge enhancement\n");
  195. X       fprintf(stderr, "         5 -- dot diffusion for grey scale with edge enhancement\n");
  196. X       fprintf(stderr, "         6 -- error diffusion for color\n");
  197. X       fprintf(stderr, "         7 -- ordered dither for color\n");
  198. X       fprintf(stderr, "         8 -- dot diffusion for color\n");
  199. X       fprintf(stderr, "         9 -- error diffusion for color with edge enhancement\n");
  200. X       fprintf(stderr, "        10 -- ordered dither for color with edge enhancement\n");
  201. X       fprintf(stderr, "        11 -- dot diffusion for color with edge enhancement\n");
  202. X       exit(1);
  203. X    }
  204. X
  205. X    sscanf(argv[1], "%d", &option);
  206. X
  207. X    /* open the input file if specified */
  208. X    if ((argc > 2) && (freopen(argv[2], "r", stdin) == NULL)) {
  209. X       fprintf(stderr, "can't open infile %s for read!\n", argv[2]);
  210. X       exit(1);
  211. X    }
  212. X
  213. X    /* open the output file if specified */
  214. X    if ((argc > 3) && (freopen(argv[3], "w", stdout) == NULL)) {
  215. X       fprintf(stderr, "can't open outfile %s for write!\n", argv[3]);
  216. X       exit(1);
  217. X    }
  218. X
  219. X    /* Load the input rasterfile header */
  220. X    if (pr_load_header(stdin, &rh)) {
  221. X       fprintf(stderr, "read rasterfile header error\n");
  222. X       exit(1);
  223. X    }
  224. X       
  225. X    if (rh.ras_depth != 8) {
  226. X       fprintf(stderr, "input file is not a 8 bits deep\n");
  227. X       exit(1);
  228. X    }
  229. X
  230. X    /* Load the colormap */
  231. X    colormap.type = RMT_NONE;
  232. X    if (pr_load_colormap(stdin, &rh, &colormap)) {
  233. X       fprintf(stderr, "read rasterfile header error\n");
  234. X       exit(1);
  235. X    }
  236. X
  237. X    if (colormap.type != RMT_NONE &&
  238. X    (colormap.type != RMT_EQUAL_RGB || colormap.length < 256)) {
  239. X       fprintf(stderr,"input has unsupported colormap type or length\n");
  240. X       exit(1);
  241. X    }
  242. X
  243. X    if ((colormap.type == RMT_NONE) && (colormap.length == 0)) {
  244. X       default_map = TRUE;
  245. X       map = (unsigned char *) malloc(256);
  246. X       for (i=0; i<256; i++)
  247. X        map[i] = i;
  248. X    }
  249. X       
  250. X    if (rh.ras_type != RT_OLD && rh.ras_type != RT_STANDARD &&
  251. X    !(pr = pr_load_image(stdin, &rh, &colormap))) {
  252. X       fprintf(stderr, "error reading rasterfile\n");
  253. X       exit(1);
  254. X    }
  255. X
  256. X    /* Write new header */
  257. X    rh.ras_type = RT_STANDARD;
  258. X    rh.ras_depth = 1;
  259. X    rh.ras_length = mpr_linebytes(rh.ras_width, 1) * rh.ras_height;
  260. X    rh.ras_maptype = RMT_NONE;
  261. X    rh.ras_maplength = 0;
  262. X
  263. X    if (pr_dump_header(stdout, &rh, (colormap_t *) 0) == PIX_ERR) {
  264. X       fprintf(stderr, "error saving raster file header!\n");
  265. X       exit(1);
  266. X    }
  267. X
  268. X    if (rh.ras_width <= 0 || rh.ras_height <= 0)
  269. X       exit(1);
  270. X    else {
  271. X        A = (short **) malloc((rh.ras_width+3)*sizeof(short *));
  272. X        B = (short **) malloc((rh.ras_width+3)*sizeof(short *));
  273. X        for(i=0; i<rh.ras_width+3; i++) {
  274. X           A[i] = (short *)malloc((rh.ras_height+3)*sizeof(short));
  275. X           B[i] = (short *)malloc((rh.ras_height+3)*sizeof(short));
  276. X        }
  277. X    }
  278. X
  279. X    if (!default_map)
  280. X       map = colormap.map[0];
  281. X
  282. X    if (pr)
  283. X       switch (option) {
  284. X          case 6  : map = (unsigned char *) malloc(256);
  285. X            color_map_3_to_1(colormap, map); 
  286. X          case 0  :    error_diffusion_image(rh.ras_width, rh.ras_height,
  287. X            map, (u_char *) mpr_d(pr)->md_image, stdout, 
  288. X            0, mpr_d(pr)->md_linebytes - rh.ras_width);
  289. X            break;
  290. X          case 7  : map = (unsigned char *) malloc(256);
  291. X            color_map_3_to_1(colormap, map);
  292. X          case 1  : ordered_dither_image(rh.ras_width, rh.ras_height,
  293. X            map, (u_char *) mpr_d(pr)->md_image, stdout,
  294. X            0, mpr_d(pr)->md_linebytes - rh.ras_width);
  295. X            break;
  296. X          case 8  : map = (unsigned char *) malloc(256);
  297. X            color_map_3_to_1(colormap, map);
  298. X          case 2  : dot_diffusion_image(rh.ras_width, rh.ras_height,
  299. X            map, (u_char *) mpr_d(pr)->md_image, stdout, 
  300. X            0, mpr_d(pr)->md_linebytes - rh.ras_width);
  301. X            break;
  302. X          case 9  : map = (unsigned char *) malloc(256);
  303. X            color_map_3_to_1(colormap, map);
  304. X          case 3  :    error_diffusion_image(rh.ras_width, rh.ras_height,
  305. X            map, (u_char *) mpr_d(pr)->md_image, stdout, 
  306. X            1, mpr_d(pr)->md_linebytes - rh.ras_width);
  307. X            break;
  308. X          case 10 : map = (unsigned char *) malloc(256);
  309. X            color_map_3_to_1(colormap, map);
  310. X          case 4  : ordered_dither_image(rh.ras_width, rh.ras_height,
  311. X            map, (u_char *) mpr_d(pr)->md_image, stdout, 
  312. X            1, mpr_d(pr)->md_linebytes - rh.ras_width);
  313. X            break;
  314. X          case 11 : map = (unsigned char *) malloc(256);
  315. X            color_map_3_to_1(colormap, map);
  316. X          case 5  : dot_diffusion_image(rh.ras_width, rh.ras_height,
  317. X            map, (u_char *) mpr_d(pr)->md_image, stdout, 
  318. X            1, mpr_d(pr)->md_linebytes - rh.ras_width);
  319. X            break;
  320. X       }
  321. X    else
  322. X       switch (option) {
  323. X          case 6  : map = (unsigned char *) malloc(256);
  324. X            color_map_3_to_1(colormap, map);
  325. X          case 0  : error_diffusion_file(rh.ras_width, rh.ras_height,
  326. X            map, stdin, stdout, 0,
  327. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  328. X            break;
  329. X          case 7  : map = (unsigned char *) malloc(256);
  330. X            color_map_3_to_1(colormap, map);
  331. X          case 1  : ordered_dither_file(rh.ras_width, rh.ras_height,
  332. X            map, stdin, stdout, 0,
  333. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  334. X            break;
  335. X          case 8  : map = (unsigned char *) malloc(256);
  336. X            color_map_3_to_1(colormap, map);
  337. X          case 2  : dot_diffusion_file(rh.ras_width, rh.ras_height,
  338. X            map, stdin, stdout, 0, 
  339. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  340. X            break;
  341. X          case 9  : map = (unsigned char *) malloc(256);
  342. X            color_map_3_to_1(colormap, map);
  343. X          case 3  : error_diffusion_file(rh.ras_width, rh.ras_height,
  344. X            map, stdin, stdout, 1,
  345. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  346. X            break;
  347. X          case 10 : map = (unsigned char *) malloc(256);
  348. X            color_map_3_to_1(colormap, map);
  349. X          case 4  : ordered_dither_file(rh.ras_width, rh.ras_height,
  350. X            map, stdin, stdout, 1,
  351. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  352. X            break;
  353. X          case 11 : map = (unsigned char *) malloc(256);
  354. X            color_map_3_to_1(colormap, map);
  355. X          case 5  : dot_diffusion_file(rh.ras_width, rh.ras_height,
  356. X            map, stdin, stdout, 1,
  357. X            mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  358. X            break;
  359. X       }
  360. X}
  361. X
  362. Xcolor_map_3_to_1(colormap, map)
  363. Xregister colormap_t    colormap;
  364. Xregister unsigned char    *map;
  365. X{
  366. Xregister unsigned long    tmp, i;
  367. X
  368. X    for (i=0; i<256; i++) {
  369. X       tmp = colormap.map[0][i]*77 + colormap.map[1][i]*151 + colormap.map[2][i]*28;
  370. X       map[i] = tmp >> 8;
  371. X    }
  372. X}
  373. X
  374. X
  375. Xedge_enhancement(width, height, off)
  376. Xregister int    width, height, off;
  377. X{
  378. Xregister int    i, j, x, y;
  379. X
  380. X    B[off][off] = A[off][off]*6 - A[off][off+1] - A[off+1][off] - A[off+1][off+1];
  381. X    B[off][off+height-1] = A[off][off+height-1]*6 - A[off][off+height-2] - A[off+1][off+height-1] - A[off+1][off+height-2];
  382. X    B[off+width-1][off] = A[off+width-1][off]*6 - A[off+width-1][off+1] - A[off+width-2][off] - A[off+width-2][off+1];
  383. X    B[off+width-1][off+height-1] = A[off+width-1][off+height-1]*6 - A[off+width-1][off+height-2] - A[off+width-2][off+height-1] - A[off+width-2][off+height-2];
  384. X
  385. X    for (i=1; i<width-1; i++)
  386. X       B[off+i][off+0] = A[off+i][off+0]*6 - A[off+i-1][off+0] - A[off+i+1][off+0] - A[off+i-1][off+1] - A[off+i][off+1] - A[off+i+1][off+1];
  387. X    for (i=1; i<width-1; i++)
  388. X       B[off+i][off+height-1] = A[off+i][off+height-1]*6 - A[off+i-1][off+height-1] - A[off+i+1][off+height-2] - A[off+i-1][off+height-2] - A[off+i][off+1] - A[off+i+1][off+height-2];
  389. X    for (i=1; i<height-1; i++)
  390. X       B[off+0][off+i] = A[off+0][off+i]*6 - A[off+0][off+i-1] - A[off+0][off+i+1] - A[off+1][off+i-1] - A[off+1][off+i] - A[off+1][off+i+1];
  391. X    for (i=1; i<height-1; i++)
  392. X       B[off+width-1][off+i] = A[off+width-1][off+i]*6 - A[off+width-1][off+i-1] - A[off+width-1][off+i+1] - A[off+width-2][off+i-1] - A[off+width-2][off+i] - A[off+width-2][off+i+1];
  393. X
  394. X    for(i=1; i<width-1; i++)
  395. X       for(j=1; j<height-1; j++) {
  396. X        B[off+i][off+j] = A[off+i][off+j]*10;
  397. X        for(x=i-1; x<=i+1; x++)
  398. X           for(y=j-1; y<=j+1; y++)
  399. X            B[off+i][off+j] -= A[off+x][off+y];
  400. X       }
  401. X
  402. X    for(i=0; i<width; i++)
  403. X       for(j=0; j<height; j++)
  404. X        A[off+i][off+j] = B[off+i][off+j];
  405. X}
  406. X
  407. X
  408. Xint index(x, y, w, pad)
  409. Xregister int    x, y, w, pad;
  410. X{
  411. X    return(x+y*(w+pad));
  412. X}
  413. X
  414. Xerror_diffusion(width, height, out, edge)
  415. Xregister int    width, height;
  416. Xregister FILE    *out;
  417. Xregister int    edge;
  418. X{
  419. Xregister int        err, mono_pad;
  420. Xregister u_short    dtmp;
  421. Xregister u_long        i, j;
  422. X
  423. X    mono_pad = mpr_linebytes(width, 1)*8 - width;
  424. X       
  425. X    if (edge)
  426. X       edge_enhancement(width, height, 1);
  427. X
  428. X    for(j=1; j<height+1; j++) {
  429. X       for(i=1; i<width+1; i++) {
  430. X        dtmp <<= 1;
  431. X        if (A[i][j] > 128)
  432. X           err = A[i][j] - 256;
  433. X        else {
  434. X           err = A[i][j];
  435. X           dtmp |= 1;
  436. X        }
  437. X        A[i+1][j] += (err*ALPHA) >> 4;
  438. X          A[i-1][j+1] += (err*BETA) >> 4;
  439. X        A[i][j+1] += (err*GAMMA) >> 4;
  440. X        A[i+1][j+1] += (err*DELTA) >> 4;
  441. X        if (((i-1) % 16) == 15) {
  442. X           putc(dtmp >> 8, out);
  443. X           putc(dtmp, out);
  444. X        }
  445. X       }
  446. X       for(i=width+1; i<=width+mono_pad; i++) {
  447. X        dtmp <<= 1;
  448. X        if (((i-1) % 16) == 15) {
  449. X           putc(dtmp >> 8, out);
  450. X           putc(dtmp, out);
  451. X        }
  452. X       }
  453. X    }
  454. X}
  455. X
  456. X/* Compute pixel values using error diffusion */
  457. Xerror_diffusion_image(width, height, map, in, out, edge_enhance, pad)
  458. Xregister int         width, height;
  459. Xregister unsigned char     *map;
  460. Xregister u_char     *in;
  461. Xregister FILE         *out;
  462. Xregister int         edge_enhance, pad;
  463. X{
  464. Xregister int    i, j;
  465. X
  466. X    for(j=1; j<height+1; j++)
  467. X       for(i=1; i<width+1; i++)
  468. X          A[i][j] = map[*(in+index(i-1, j-1, width, pad))];
  469. X
  470. X    error_diffusion(width, height, out, edge_enhance);
  471. X}
  472. X
  473. X
  474. X/* Compute pixel values using error diffusion */
  475. Xerror_diffusion_file(width, height, map, in, out, edge_enhance, pad)
  476. Xregister int         width, height;
  477. Xregister unsigned char     *map;
  478. Xregister FILE         *in, *out;
  479. Xregister int         edge_enhance, pad;
  480. X{
  481. Xregister int        i, j, c; 
  482. X
  483. X    for(j=1; j<height+1; j++) {
  484. X       for(i=1; i<width+1; i++) {
  485. X        if ((c = getc(in)) == EOF) {
  486. X           fprintf(stderr, "error reading raster data!\n");
  487. X           exit(1);
  488. X        }
  489. X        A[i][j] = map[c];
  490. X       }
  491. X       for(i=width; i<width+pad; i++)
  492. X        if ((c = getc(in)) == EOF)
  493. X           fprintf(stderr, "error reading raster data!\n");
  494. X    }
  495. X
  496. X    error_diffusion(width, height, out, edge_enhance);
  497. X}
  498. X
  499. Xordered(width, height, out, edge)
  500. Xregister int    width, height;
  501. Xregister FILE    *out;
  502. Xregister int    edge;
  503. X{
  504. Xregister int        mono_pad;
  505. Xregister u_short    dtmp;
  506. Xregister u_long        i, j;
  507. X
  508. X    mono_pad = mpr_linebytes(width, 1)*8 - width;
  509. X
  510. X    if (edge)
  511. X       edge_enhancement(width, height, 0);
  512. X
  513. X    for(j=0; j<8; j++)
  514. X       for(i=0; i<8; i++)
  515. X        ordered_dither[i][j] = ordered_dither[i][j]*4 + 2;
  516. X
  517. X    for(j=0; j<height; j++) {
  518. X       for(i=0; i<width; i++) {
  519. X        dtmp <<= 1;
  520. X        if (A[i][j] >= ordered_dither[i%8][j%8])
  521. X           dtmp |= 1;
  522. X        if ((i % 16) == 15) {
  523. X           putc(dtmp >> 8, out);
  524. X           putc(dtmp, out);
  525. X        }
  526. X       }
  527. X       for(i=width; i<width+mono_pad; i++) {
  528. X        dtmp <<= 1;
  529. X        if ((i % 16) == 15) {
  530. X           putc(dtmp >> 8, out);
  531. X           putc(dtmp, out);
  532. X        }
  533. X       }
  534. X    }
  535. X}                    
  536. X                    
  537. Xordered_dither_image(width, height, map, in, out, edge_enhance, pad)
  538. Xregister int         width, height;
  539. Xregister unsigned char     *map;
  540. Xregister u_char     *in;
  541. Xregister FILE         *out;
  542. Xregister int         edge_enhance, pad;
  543. X{
  544. Xregister int        i, j;
  545. X
  546. X    for(j=0; j<height; j++)
  547. X       for(i=0; i<width; i++)
  548. X          A[i][j] = 256 - map[*(in+index(i, j, width, pad))];
  549. X
  550. X    ordered(width, height, out, edge_enhance);
  551. X}
  552. X
  553. X
  554. Xordered_dither_file(width, height, map, in, out, edge_enhance, pad)
  555. Xregister int         width, height;
  556. Xregister unsigned char     *map;
  557. Xregister FILE         *in, *out;
  558. Xregister int         edge_enhance, pad;
  559. X{
  560. Xregister int        i, j, c;
  561. X
  562. X    for(j=0; j<height; j++) {
  563. X       for(i=0; i<width; i++) {
  564. X          if ((c = getc(in)) == EOF) {
  565. X         fprintf(stderr, "error reading raster data!\n");
  566. X         exit(1);
  567. X          }
  568. X          A[i][j] = 256 - map[c];
  569. X       }
  570. X       for(i=width; i<width+pad; i++)
  571. X        if ((c = getc(in)) == EOF)
  572. X           fprintf(stderr, "error reading raster data!\n");
  573. X    }
  574. X
  575. X    ordered(width, height, out, edge_enhance);
  576. X}
  577. X
  578. Xint weight(x, y)
  579. Xregister int    x,y;
  580. X{
  581. X    return(3 - x*x - y*y);
  582. X}
  583. X
  584. Xdot(width, height, out, edge)
  585. Xregister int    width, height;
  586. Xregister FILE    *out;
  587. Xregister int    edge;
  588. X{
  589. Xregister int        k, u, v, err, w, mono_pad;
  590. Xregister u_short    dtmp;
  591. Xregister u_long        i, j;
  592. X
  593. X    mono_pad = mpr_linebytes(width, 1)*8 - width;
  594. X
  595. X    if (edge)
  596. X       edge_enhancement(width, height, 0);
  597. X
  598. X    for(k=0; k<64; k++) {
  599. X       for(i=reverse_matrix[k]/8; i<width; i+=8)
  600. X          for(j=reverse_matrix[k]%8; j<height; j+=8) {
  601. X         if (A[i][j] > 128)
  602. X            B[i][j] = 0;
  603. X         else
  604. X            B[i][j] = 1;
  605. X         err = A[i][j] - (1 - B[i][j])*256;
  606. X         w = 0;
  607. X         for(u=i-1; u<=i+1; u++)
  608. X            for(v=j-1; v<=j+1; v++)
  609. X               if ((u>=0) && (v>=0) && (dot_diffusion[u%8][v%8] > k))
  610. X              w += weight(u-i, v-j);
  611. X         if (w > 0) {
  612. X            for(u=i-1; u<=i+1; u++)
  613. X               for(v=j-1; v<=j+1; v++)
  614. X              if ((u>=0)&&(v>=0)&&(dot_diffusion[u%8][v%8]>k))
  615. X                 A[u][v] += err * weight(u-i, v-j) / w;
  616. X         }
  617. X          }
  618. X    }
  619. X
  620. X    for(j=0; j<height; j++) {
  621. X       for(i=0; i<width; i++) {
  622. X        dtmp <<= 1;
  623. X        if (B[i][j])
  624. X           dtmp |= 1;
  625. X           if ((i % 16) == 15) {
  626. X           putc(dtmp >> 8, out);
  627. X           putc(dtmp, out);
  628. X        }
  629. X       }
  630. X       for(i=width; i<width+mono_pad; i++) {
  631. X        dtmp <<= 1;
  632. X        if ((i % 16) == 15) {
  633. X           putc(dtmp >> 8, out);
  634. X           putc(dtmp, out);
  635. X        }
  636. X       }
  637. X    }
  638. X}
  639. X
  640. X
  641. Xdot_diffusion_image(width, height, map, in, out, edge_enhance, pad)
  642. Xregister int         width, height;
  643. Xregister unsigned char     *map;
  644. Xregister u_char     *in;
  645. Xregister FILE         *out;
  646. Xregister int         edge_enhance, pad;
  647. X{
  648. Xregister int        i, j;
  649. X
  650. X    for(j=0; j<height; j++)
  651. X       for(i=0; i<width; i++)
  652. X          A[i][j] = map[*(in+index(i, j, width, pad))];
  653. X
  654. X    dot(width, height, out, edge_enhance);
  655. X}
  656. X
  657. Xdot_diffusion_file(width, height, map, in, out, edge_enhance, pad)
  658. Xregister int         width, height;
  659. Xregister unsigned char     *map;
  660. Xregister FILE         *in, *out;
  661. Xregister int         edge_enhance, pad;
  662. X{
  663. Xregister int        i, j, c;
  664. X
  665. X    for(j=0; j<height; j++) {
  666. X       for(i=0; i<width; i++) {
  667. X          if ((c = getc(in)) == EOF) {
  668. X         fprintf(stderr, "error reading raster data!\n");
  669. X         exit(1);
  670. X          }
  671. X          A[i][j] = map[c];
  672. X       }
  673. X       for(i=width; i<width+pad; i++)
  674. X        if ((c = getc(in)) == EOF)
  675. X           fprintf(stderr, "error reading raster data!\n");
  676. X    }
  677. X
  678. X    dot(width, height, out, edge_enhance);
  679. X}
  680. X
  681. END_OF_8to1.c
  682. if test 16939 -ne `wc -c <8to1.c`; then
  683.     echo shar: \"8to1.c\" unpacked with wrong size!
  684. fi
  685. # end of overwriting check
  686. fi
  687. if test -f scload.c -a "${1}" != "-c" ; then 
  688.   echo shar: Will not over-write existing file \"scload.c\"
  689. else
  690. echo shar: Extracting \"scload.c\" \(6462 characters\)
  691. sed "s/^X//" >scload.c <<'END_OF_scload.c'
  692. X/* Compile: cc -g -o scload scload.c -lpixrect */
  693. X
  694. X/**************************************************************************
  695. X   The program "scload" is like Sun's "screenload" program, but more
  696. X   entertaining, and actually better than screenload.
  697. X
  698. X   Copyright (c) 1988 by Raymond Kreisel
  699. X   7/1/88 @ Suny Stony Brook
  700. X
  701. X   This program may be redistributed without fee as long as this copyright
  702. X   notice is intact.
  703. X
  704. X==> PLEASE send comments and bug reports to one of the following addresses:
  705. X
  706. X       Ray Kreisel
  707. X       CS Dept., SUNY at Stony Brook, Stony Brook NY 11794
  708. X
  709. X       UUCP: {allegra, philabs, pyramid, research}!sbcs!rayk   
  710. X       ARPA-Internet: rayk@sbcs.sunysb.edu            
  711. X       CSnet: rayk@suny-sb
  712. X       (If nobody is home at any of the above addresses try:
  713. X        S72QKRE@TOWSONVX.BITNET                    )
  714. X
  715. X "If I get home before daylight, I just might get some sleep tonight...."
  716. X
  717. X**************************************************************************/
  718. X#include <pixrect/pixrect_hs.h>
  719. X#include <stdio.h>
  720. X#include <sys/time.h>
  721. X#include <sys/types.h>
  722. X#include <sys/stat.h>
  723. X
  724. X
  725. Xint time_rand()
  726. X{
  727. X struct tm *localtime(), *nowtime;
  728. X long inttime;
  729. X
  730. X inttime = time();
  731. X nowtime = localtime(&inttime);
  732. X return((int)((nowtime->tm_sec)));
  733. X}
  734. X
  735. X
  736. Xmain(argc,argv) int argc; char *argv[];
  737. X{
  738. Xint i, my_fb = 0;
  739. Xchar *fb, *strcpy();
  740. X
  741. X   if (argc == 1) {
  742. X       fprintf(stderr,"Usage: scload [-f frame_buffer] filename\n");
  743. X    exit(1);
  744. X   }
  745. X
  746. X   if ((argv[1][0] == '-') && (argv[1][1] == 'f')) {
  747. X    if (argc < 4) {
  748. X           fprintf(stderr,"Usage: scload [-f frame_buffer] filename\n");
  749. X       exit(1);
  750. X    }
  751. X    fb = strcpy((char *)malloc(strlen(argv[2])+1), argv[2]);
  752. X    my_fb = 1;
  753. X   }
  754. X   else fb = strcpy((char *)malloc(8), "/dev/fb");
  755. X   if (argc)
  756. X   {
  757. X   for(i=1+my_fb*2;i<argc;i++)
  758. X     new_sc_load(argv[i], fb);
  759. X   }
  760. X   else
  761. X     new_sc_load("", fb);
  762. X}
  763. X
  764. X
  765. Xnew_sc_load(filename, fb)
  766. Xchar *filename, *fb;
  767. X{
  768. X  char colormapname[256];
  769. X  struct rasterfile rh;
  770. X  colormap_t colormap;
  771. X  FILE *fp,*fopen();
  772. X  int    i, j,width,height,off_x,off_y;
  773. X  struct pixrect    *line, *screen, *screen_temp;
  774. X  int sleep_time=500;
  775. X
  776. X    srand(time_rand());
  777. X    if (filename[0])
  778. X    {
  779. X        fp = fopen(filename,"r");
  780. X        if (!fp)
  781. X        {
  782. X          fprintf(stderr,"error open file %s for read\n", filename);
  783. X          return;
  784. X        }
  785. X    }
  786. X    else
  787. X      fp = stdin;
  788. X
  789. X    screen = pr_open(fb);
  790. X    if (!screen)
  791. X       exit(1);
  792. X
  793. X    if (screen->pr_depth > 1)
  794. X    {
  795. X      sleep_time = 0;
  796. X      if (pr_load_header(fp,&rh)) {
  797. X        perror("scload: pr_load_header");
  798. X        exit(-1);
  799. X        };
  800. X      if (pr_load_colormap(fp,&rh,&colormap)) {
  801. X        perror("scload: pr_load_colormap");
  802. X        exit(-1);
  803. X        };
  804. X      if (!(screen_temp=pr_load_image(fp,&rh,&colormap))) {
  805. X         perror("scload: pr_load_image");
  806. X         exit(-1);
  807. X         };
  808. X      if (colormap.length)
  809. X        pr_putcolormap(screen,0,rh.ras_maplength/3,colormap.map[0],
  810. X                     colormap.map[1],colormap.map[2]);
  811. X  
  812. X    }
  813. X    else
  814. X    {
  815. X        screen_temp = pr_load(fp,NULL);
  816. X        if (!screen_temp)
  817. X        {
  818. X            fprintf(stderr,"Error reading rasterfile header.\n");
  819. X            exit(0);
  820. X        }
  821. X    }
  822. X    width = screen_temp->pr_size.x;
  823. X    height = screen_temp->pr_size.y;
  824. X    if (screen->pr_size.x > screen_temp->pr_size.x)
  825. X       off_x = (screen->pr_size.x - screen_temp->pr_size.x)/2;
  826. X    if (screen->pr_size.y > screen_temp->pr_size.y)
  827. X       off_y = (screen->pr_size.y - screen_temp->pr_size.y)/2;
  828. X
  829. X    switch(rand()%8) {  
  830. X    case 0:
  831. X        for (j=height-16; j > 0; j-=16)
  832. X        {
  833. X           pr_rop(screen,off_x,j+off_y,width,height-j,PIX_SRC,screen_temp,0,0);
  834. X           usleep(sleep_time);
  835. X        }
  836. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  837. X        
  838. X        break;
  839. X    case 1:
  840. X
  841. X        for (j=0; j < height; j+=32)
  842. X           for (i=0; i < width; i+=32)
  843. X              pr_rop(screen,i+off_x,j+off_y,16,16,PIX_SRC,screen_temp,i,j);
  844. X        for (j=16; j < height; j+=32)
  845. X           for (i=16; i < width; i+=32)
  846. X              pr_rop(screen,i+off_x,j+off_y,16,16,PIX_SRC,screen_temp,i,j);
  847. X        for (j=16; j < height; j+=32)
  848. X           for (i=0; i < width; i+=32)
  849. X              pr_rop(screen,i+off_x,j+off_y,16,16,PIX_SRC,screen_temp,i,j);
  850. X        for (j=0; j < height; j+=32)
  851. X           for (i=16; i < width; i+=32)
  852. X              pr_rop(screen,i+off_x,j+off_y,16,16,PIX_SRC,screen_temp,i,j);
  853. X        break;
  854. X
  855. X    case 2:
  856. X        for (j=height-16; j > 0; j-=16)
  857. X        {
  858. X       
  859. X           pr_rop(screen,off_x,off_y,width,height-j,PIX_SRC,screen_temp,0,j);
  860. X           usleep(sleep_time);
  861. X        }
  862. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  863. X        break;
  864. X
  865. X    case 3:
  866. X        for (j=height-16; j > 0; j-=16)
  867. X        {
  868. X       
  869. X           pr_rop(screen,off_x,j+off_y,width/2,height-j,PIX_SRC,screen_temp,0,0);
  870. X           pr_rop(screen,off_x+width/2,off_y,width/2,height-j,PIX_SRC,screen_temp,width/2,j);
  871. X           usleep(sleep_time);
  872. X        }
  873. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  874. X        break;
  875. X    case 4:
  876. X        for (j=height-16; j > 0; j-=16)
  877. X        {
  878. X       
  879. X           pr_rop(screen,off_x+width/2,j+off_y,width/2,height-j,PIX_SRC,screen_temp,width/2,0);
  880. X           pr_rop(screen,off_x,off_y,width/2,height-j,PIX_SRC,screen_temp,0,j);
  881. X           usleep(sleep_time);
  882. X        }
  883. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  884. X        break;
  885. X
  886. X    case 5:
  887. X        for (j=height-16; j > 0; j-=16)
  888. X        {
  889. X       
  890. X           pr_rop(screen,off_x,off_y,width/4,height-j,PIX_SRC,screen_temp,0,j);
  891. X           pr_rop(screen,off_x+width/4,j+off_y,width/4+2,height-j,PIX_SRC,screen_temp,width/4,0);
  892. X           pr_rop(screen,off_x+width/4*3,j+off_y,width/4,height-j,PIX_SRC,screen_temp,width/4*3,0);
  893. X           pr_rop(screen,off_x+width/2,off_y,width/4,height-j,PIX_SRC,screen_temp,width/2,j);
  894. X           usleep(sleep_time);
  895. X        }
  896. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  897. X        break;
  898. X    case 6:
  899. X        for (j=height-16; j > 0; j-=16)
  900. X        {
  901. X       
  902. X           pr_rop(screen,off_x,off_y,width/4,height-j,PIX_SRC,screen_temp,0,j);
  903. X           pr_rop(screen,off_x+width/4,j+off_y,width/4,height-j,PIX_SRC,screen_temp,width/4,0);
  904. X           pr_rop(screen,off_x+width/2,j+off_y,width/4,height-j,PIX_SRC,screen_temp,width/2,0);
  905. X           pr_rop(screen,off_x+width/4*3,off_y,width/4,height-j,PIX_SRC,screen_temp,width/4*3,j);
  906. X           usleep(sleep_time);
  907. X        }
  908. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  909. X        break;
  910. X
  911. X    case 7:
  912. X        for (j=16; j < width/2; j+=16)
  913. X        {
  914. X       
  915. X           pr_rop(screen,off_x+width/2-j,off_y,j,height,PIX_SRC,screen_temp,0,0);
  916. X           pr_rop(screen,off_x+width/2,off_y,j,height,PIX_SRC,screen_temp,width-j,0);
  917. X           usleep(sleep_time);
  918. X        }
  919. X        pr_rop(screen,off_x,off_y,width,height,PIX_SRC,screen_temp,0,0);
  920. X        break;
  921. X    }
  922. X
  923. X    pr_destroy(screen_temp);
  924. X    pr_close(screen);
  925. X        fclose(fp);
  926. X}
  927. X
  928. END_OF_scload.c
  929. if test 6462 -ne `wc -c <scload.c`; then
  930.     echo shar: \"scload.c\" unpacked with wrong size!
  931. fi
  932. # end of overwriting check
  933. fi
  934. echo shar: End of archive 1 \(of 1\).
  935. cp /dev/null ark1isdone
  936. MISSING=""
  937. for I in 1 ; do
  938.     if test ! -f ark${I}isdone ; then
  939.     MISSING="${MISSING} ${I}"
  940.     fi
  941. done
  942. if test "${MISSING}" = "" ; then
  943.     echo You have unpacked all 1 archives.
  944.     rm -f ark[1-9]isdone
  945. else
  946.     echo You still need to unpack the following archives:
  947.     echo "        " ${MISSING}
  948. fi
  949. ##  End of shell archive.
  950. exit 0
  951.